package com.zyl.widget.timer;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class BaseConf extends Activity {
	int mAppWidgetId = 0;

	TimePicker timePicker;
	EditText title;

	Button submitButton;
	Button cancelButton;

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setResult(0);
		setContentView(R.layout.configure);
		init();
		Bundle localBundle = getIntent().getExtras();
		if (localBundle != null)
			this.mAppWidgetId = localBundle.getInt("appWidgetId", 0);
		if (this.mAppWidgetId == 0)
			finish();
	}

	private void init() {
		this.title = ((EditText) findViewById(R.id.Type));
		this.timePicker = ((TimePicker) findViewById(R.id.datePicker1));
		this.timePicker.setIs24HourView(true);
		this.timePicker.setCurrentHour(0);
		this.timePicker.setCurrentMinute(0);
		this.submitButton = ((Button) findViewById(R.id.submitButton));
		this.submitButton.setOnClickListener(this.submitOnClickListener);
		this.cancelButton = ((Button) findViewById(R.id.cancelBtn));
		this.cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				BaseConf.this.finish();
			}
		});
	}

	View.OnClickListener submitOnClickListener = new View.OnClickListener() {
		public void onClick(View paramView) {
			BaseConf context = BaseConf.this;
			String str1 = BaseConf.this.title.getText().toString();
			long ms = BaseConf.this.timePicker.getCurrentHour()*60*60*1000 
			+ BaseConf.this.timePicker.getCurrentMinute()*60*1000  ;
			
			BaseConf.savePref(context, str1, ms  );
			BaseWidget.updateAppWidget(context,
					AppWidgetManager.getInstance(context),
					BaseConf.this.mAppWidgetId);
			BaseWidget.time= ms;
			Intent localIntent = new Intent();
			localIntent.putExtra("appWidgetId", BaseConf.this.mAppWidgetId);
			BaseConf.this.setResult(-1, localIntent);
			BaseConf.this.finish();
		}
	};
	static String CONFIG="com.zyl.widget.timer.config";
	static String[] loadPref(Context context ) {
		String[] config = new String[2];
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				CONFIG, 0);
		config[0] = sharedPreferences.getString("title" , "");
		config[1] = sharedPreferences.getString("time" ,"0");
		return config;
	}

	static void savePref(Context context, String title,
			long date) {
		SharedPreferences.Editor editor = context.getSharedPreferences(
				CONFIG, 0).edit();
		editor.putString("title" , title);
		editor.putString("time"  , date+"");
		 
		editor.commit();
	}

}
